public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }


    /**
     * 创建一棵二叉树 返回这棵树的根节点
     *
     * @return
     */
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;

    }

    //相同的树
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q != null || p != null && q == null){
            return false;
        }
        if(p == null && q == null){
            return true;
        }
        if(p.val != q.val){
            return false;
        }
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }

    //翻转二叉树
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return null;
        TreeNode tmp;
        tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    //对称二叉树
    public boolean isSymmetric(TreeNode root) {
        if(root == null) return false;
        return cmp(root.left,root.right);
    }

    public boolean cmp(TreeNode leftTree,TreeNode rightTree){
        if(leftTree == null && rightTree != null || leftTree != null && rightTree == null) return false;
        if(leftTree == null && rightTree == null) return true;
        if(leftTree.val != rightTree.val){
            return false;
        }
        return cmp(leftTree.left,rightTree.right) && cmp(leftTree.right,rightTree.left);
    }

    //另一颗树的子树
//    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
//        if(root == null){
//            return false;
//        }
//        if(isSameTree(root,subRoot)){
//            return true;
//        }
//        if(isSubtree(root.left,subRoot)){
//            return true;
//        }
//        if(isSubtree(root.right,subRoot)){
//            return true;
//        }
//        return false;
//    }
//
//    public boolean isSameTree(TreeNode p, TreeNode q) {
//        if(p == null && q != null || p != null && q == null){
//            return false;
//        }
//        if(p == null && q == null){
//            return true;
//        }
//        if(p.val != q.val){
//            return false;
//        }
//        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
//    }

    //平衡二叉树
    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;
        return getHigh2(root) >= 0;
    }

    public int getHigh2(TreeNode root){
        if(root == null) return 0;
        int leftHigh = getHigh2(root.left);
        if(leftHigh < 0){
            return -1;
        }
        int rightHigh = getHigh2(root.right);
        if(leftHigh >= 0 && rightHigh >= 0 && Math.abs(leftHigh - rightHigh) <= 1){
            return Math.max(leftHigh,rightHigh) + 1;
        }else{
            return -1;
        }
    }
}
